<?php

namespace app\admin\controller;

use think\Db;
use think\Request;

class Coupon extends Common
{

    // 优惠券分类管理
    public function cate()
    {
        if (request()->isPost()) {
            $data = input('post.');

            $page = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : config('common.page_sizes');
            $list = db('coupon_cate')
                ->order('id desc')
                ->paginate(array('list_rows' => $pageSize, 'page' => $page))
                ->toArray();
            $list = model('couponCate')->listAddTimeInfo($list);
            return $result = ['code' => 0, 'msg' => config('common.data_success'), 'data' => $list['data'], 'count' => $list['total'], 'rel' => 1];
        }
        return $this->fetch('');
    }
    // 更变优惠券状态
    public function cateStatus()
    {
        if (request()->isPost()) {
            $data = input('post.');
            return model('couponCate')->commonState($data, '状态更变成功', '状态更变失败，请稍后重试');

        }
    }
    // 优惠券分类删除
    public function cateDel()
    {
        if (request()->isPost()) {
            $data = input('post.')['id'];
            return model('couponCate')->commonDel($data, '删除成功', '删除失败，请重试');
        }
    }
    // 优惠券分类编辑
    public function cateEdit()
    {
        if (request()->isPost()) {
            $data = input('post.');
            return model('couponCate')->commonData($data, '修改成功!', '修改失败，请重试!');
        } else {
            $id = input('id');
            $list = db('couponCate')->where(['id' => $id])->find();
            return $this->fetch('', ['list' => $list]);
        }
    }
    // 优惠券分类添加
    public function cateAdd()
    {
        if (request()->isPost()) {
            $data = input('post.');
            return model('couponCate')->commonData($data, '添加成功', '添加失败，请重试');
        } else {
            return $this->fetch('');
        }
    }

    // 优惠券管理   列表页
    public function index()
    {
        if (request()->isPost()) {
            $data = input('post.');

            $page = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : config('common.page_sizes');
            $list = db('coupon')
                ->order('id desc')
                ->paginate(array('list_rows' => $pageSize, 'page' => $page))
                ->toArray();
            $list = model('coupon')->listAddTimeInfo($list);
            foreach ($list['data'] as $i => $item) {
                $list['data'][$i]['cate_id'] = db('coupon_cate')->where(['id' => $item['cate_id']])->field('title')->find()['title'];
            }
            return $result = ['code' => 0, 'msg' => config('common.data_success'), 'data' => $list['data'], 'count' => $list['total'], 'rel' => 1];
        }
        return $this->fetch('');
    }
    // 更变优惠券状态
    public function indexStatus()
    {
        if (request()->isPost()) {
            $data = input('post.');
            return model('coupon')->commonState($data, '状态更变成功', '状态更变失败，请稍后重试');

        }
    }
    // 更变优惠券状态
    public function indexAutomatic()
    {
        if (request()->isPost()) {
            $data = input('post.');
            return model('coupon')->commonState($data, '状态更变成功', '状态更变失败，请稍后重试');

        }
    }
// 优惠券编辑
    public function edit()
    {
        if (request()->isPost()) {
            $data = input('post.');
            return model('coupon')->commonData($data, '修改成功!', '修改失败，请重试!');
        } else {
            $id = input('id');
            $list = db('coupon')->where(['id' => $id])->find();
            $list_category = db('couponCate')->where(['status' => 0])->order('id asc')->select();
            // $list['cate_id'] = db('couponCate')->where(['status' => 0, 'id' => $list['cate_id']])->find();
            return $this->fetch('', ['list' => $list, 'list_category' => $list_category]);
        }
    }
    // 优惠券分类添加
    public function add()
    {
        if (request()->isPost()) {
            $data = input('post.');
            return model('coupon')->commonData($data, '添加成功', '添加失败，请重试');
        } else {
            $list_category = db('couponCate')->where(['status' => 0])->order('id asc')->select();
            return $this->fetch('', ['list_category' => $list_category]);
        }
    }
    // 用户优惠券列表
    public function user()
    {
        if (request()->isPost()) {
            $data = input('post.');
            $phone = input('post.phone');
            $status = input('post.status');
            $where = array();
            if ($phone) {
                $where1['phone'] = $phone;
                $user_id = db('user')->where($where1)->find();
                $where['a.user_id'] = $user_id['user_id'];
            }
            if (isset($status)) {
                $where['a.status'] = $status;
            }
            $page = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : config('common.page_sizes');
            $list = db('coupon_user')
                ->alias('a')
                ->join('user u', 'a.user_id = u.user_id','left')
                ->join('vip_cate v','u.vip_id = v.id','left')
                ->where($where)
                ->field('a.id as id,u.realname as realname,u.phone as phone,v.title as vip_id,u.vip_number as vip_number,a.status as status,a.coupon_info as coupon_info,a.create_time as create_time')
                ->order('a.id desc')
                ->paginate(array('list_rows' => $pageSize, 'page' => $page))
                ->toArray();
            foreach ($list['data'] as $i => $item) {
                $uh = unserialize($item['coupon_info']);
                $uh['cate_id'] = db('coupon_cate')->where(['id'=>$uh['cate_id']])->find()['title'];
                $list['data'][$i]['cate_id'] = $uh['cate_id'];
                $list['data'][$i]['coupon_name'] = $uh['title'];
                $list['data'][$i]['create_time'] = date('Y-m-d',$item['create_time']);
            }
            
            return $result = ['code' => 0, 'msg' => config('common.data_success'), 'data' => $list['data'],'count'=>$list['total'], 'rel' => 1];
        } else {
            return $this->fetch('');
        }
    }
    // 发放优惠券

    public function userAdd()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $user_id = db('user')->where(['phone'=>$post['phone']])->find()['user_id'];
            $coupon = db('coupon')->where(['id'=>$post['coupon_id']])->find();
            
            if($coupon){
                $data = [
                    'user_id'=>$user_id,
                    'coupon_id'=>$post['coupon_id'],
                    'status'=>0,
                    'coupon_info'=>serialize($coupon),
                    'create_time'=>time(),
                    'end_time' => time()+$coupon['end_time']*24*3600
                ];
                return model('coupon_user')->commonData($data, '添加成功', '添加失败，请重试');
            }
        } else {
            $list_category = db('couponCate')->where(['status' => 0])->order('id asc')->select();
            return $this->fetch('', ['list_category' => $list_category]);
        }
    }
    // 获取优惠券
    public function getcoupon()
    {
        if(request()->isPost()){
            $data = input('post.');
            // 优惠券的状态
            $data['status'] = 0;
            // 优惠券数量
            $data['number'] = ['<>',0];
            // 优惠券发放方式
            $data['automatic'] = 0;
            $list = db('coupon')->where($data)->select();
            $time = time();
            $arr = array();
            foreach ($list as $key => $item) {
                if($item['start_time']==0 || $item['end_time']==0){
                    $arr[]= $item;
                }else{
                    $coupon_time = $item['start_time']+$item['end_time']*24*60*60;
                    if($coupon_time > $time){
                        $arr[]= $item;
                    }
                }
            }
            return $result = ['code' => 0, 'msg' => config('common.data_success'), 'data' => $arr,  'rel' => 1];

        }
    }
}
